﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PraxisRent.Entities;
using PraxisRent.BL;
using Microsoft.Practices.Unity;
using System.IO;

namespace PraxisRent.UI
{
    public partial class FrmCambiarPassword : Form
    {
        [Dependency]
        public IUsuarioBL usuarioBL { get; set; }

        [Dependency]
        public IFotoPersonaBL fotPersonaBL { get; set; }

        public FrmCambiarPassword()
        {
            InitializeComponent();
            this.BuilUp();
        }

        public USUARIO _usuario { get; set; }

        public void editarUsuario()
        {
            lblUsuario.Text = _usuario.PERSONA.NombreCompleto;
            lblDni.Text = _usuario.PERSONA.Dni;

            //cargando la foto del trabajador
            var img = fotPersonaBL.GetFromFotoPersonaByPersonaId(_usuario.PersonaId);
            if (img.Imagen != null)
            {
                MemoryStream stream = new MemoryStream(img.Imagen);
                pbFoto.Image = Image.FromStream(stream);
            }
            else
            {
                pbFoto.Image = pbFoto.ErrorImage;
            }
        }

        private void FrmCambiarPassword_Load(object sender, EventArgs e)
        {

        }

        /*función de encriptacion MD5 para password*/
        public string md5()
        {
            System.Security.Cryptography.MD5 md5;
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] encodedBytes = md5.ComputeHash(ASCIIEncoding.Default.GetBytes(passwordTextBox.Text));

            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes).ToUpper(), @"-", "");
        }

        /*función de encriptacion MD5 para verificar la password*/
        public string md5VerificarPasswd()
        {
            System.Security.Cryptography.MD5 md5;
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] encodedBytes = md5.ComputeHash(ASCIIEncoding.Default.GetBytes(txtAntiguaPasswd.Text));

            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes).ToUpper(), @"-", "");
        }

        public void verificarPsswd()
        {
            var msg = "ERROR";

            try
            {
                _usuario.Password = md5VerificarPasswd();
                usuarioBL.ValidarPasswd(_usuario);
                MessageBox.Show("PASSWORD CORRECTA", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                passwordTextBox.Text = "";
                passwordTextBox.ReadOnly = false;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void btnVerificar_Click(object sender, EventArgs e)
        {
            verificarPsswd();
        }

        private void txtAntiguaPasswd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                verificarPsswd();
            }
        }

        private void txtAntiguaPasswd_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                verificarPsswd();
            }
        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            if (passwordTextBox.Text == txtConfirPasswd.Text)
            {
                var usuario = (from u in usuarioBL.GetAllFromUsuarioByPersona(_usuario.PERSONA.Dni)
                               select u).SingleOrDefault() as USUARIO;

                usuario.Password = md5();
                usuarioBL.UpdatePasswdUsuario(usuario);
                MessageBox.Show("Cambio de Password Correcta", "Mensaje", MessageBoxButtons.OK);
                this.Close();
            }
            else
            {
                MessageBox.Show("Las Password no Coinciden", "Mensaje", MessageBoxButtons.OK);
            }
        }

        private void passwordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (passwordTextBox.Text.Length >= 6)
                {
                    txtConfirPasswd.ReadOnly = false;
                }
                else
                {
                    MessageBox.Show("Ingrese un Password mayor o igual a 6 caracteres", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void passwordTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                if (passwordTextBox.Text.Length >= 6)
                {
                    txtConfirPasswd.ReadOnly = false;
                }
                else
                {
                    MessageBox.Show("Ingrese un Password mayor o igual a 6 caracteres", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
